<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

require_once 'phpmailer/class.phpmailer.php';
require_once 'core/app/AppMailModel.php';
require_once 'core/app/AppMailParser.php';


class AppMailSender
{
	
	var $noreply_email;
	var $from_mailer;
	
	var $letter_key;
	var $settings =  array('mailer'      => 'mail',
	                       'content_type'=> 'text/plain',
						   'charset'     => 'iso-8859-1',
						   
						   'from_email'  => '',
						   'from_name'   => '',
						   'reply_to'    => array(),
						   'from_mailer' => '',
						   'sender' 	 => 'KBMailer',
						   
						   'sendmail_path' => '/usr/sbin/sendmail',
						   'smtp_host'   => 'localhost',
						   'smtp_port'   => '25',
						   'smtp_auth'   => false,
						   'smtp_user'   => '',
						   'smtp_pass'   => ''
						   );
	
	
	function AppMailSender() {
		
		$reg =& Registry::instance();
		$conf =& $reg->getEntry('conf');
		
		$this->model = &new AppMailModel();
		$this->settings = array_merge($this->settings, $this->model->getSettings());
		$this->settings['charset'] = $conf['lang']['meta_charset'];
		
		$this->parser = &new AppMailParser();
		$this->parser->setSettingVars($this->settings);
		
		$this->noreply_email = 'noreply@'.$_SERVER['HTTP_HOST'];
		$this->from_mailer = $this->settings['from_mailer'];
	}

	
	function getTemplate($from_db = true) {
		$template = false;
		//if($this->model->isExtra('letter_template') && $from_db) {
			$template = $this->model->getTemplate($this->letter_key);
		//}
	
		if(!$template) {
			$template = $this->parser->getTemplate($this->letter_key);
		}
	
		return $template;
	}
	
	
	function getTemplateVars($from_db = true) {
		
		$data = array();
		//if($this->model->isExtra('letter_template') && $from_db) {
			$data = $this->model->getTemplateVars($this->letter_key);
		//}
		
		// reading from msg
		if(empty($data['subject'])) {
			$data1 = $this->parser->getTemplateMsg($this->letter_key);
			$data['subject'] = $data1['subject'];
		}		
		
		return $data;
	}
	
	
	function &getMailerObj($template_vars, $values = array()) {
		
		$values = array_merge($this->settings, $values);
		
		$mail = new PHPMailer();
		$mail->SetLanguage("en", APP_LIB_DIR . 'phpmailer/language/');
		//$mail->SMTPDebug = true;
		$mail->CharSet = $values['charset'];
		$mail->ContentType = $values['content_type'];
		$mail->Mailer = $values['mailer'];
		$mail->Sender = $values['sender'];
		
		$mail->Sendmail = $values['sendmail_path'];
		$mail->Host = $values['smtp_host'];
		$mail->Port = $values['smtp_port'];
		//$mail->SMTPAuth = ($values['mailer'] == 'smtp');
		$mail->SMTPAuth = $values['smtp_auth'];
		$mail->Username = $values['smtp_user'];
		$mail->Password = $values['smtp_pass'];
		
		//default values
		$mail->From = $values['from_email'];
		$mail->FromName = $values['from_name'];
		
		$mail->ReplyTo = $values['reply_to'];		
		
		//echo "<pre>"; print_r($mail); echo "</pre>";
		//echo "<pre>"; print_r($values); echo "</pre>";
		//echo "<pre>"; print_r($this->parser); echo "</pre>";
		//echo "<pre>"; print_r($template_vars); echo "</pre>";
		//exit;
		
		if($template_vars) {
			
			$values = &$template_vars;
			foreach($values as $k => $v) {
				$values[$k] = $this->parser->parse($v);
			}
			
			$mail->Subject = $values['subject'];
			
			if($values['from_email']) {  
				$mail->From = $values['from_email'];
				$mail->FromName = $values['from_name'];					
			}
		
			if($values['to_email']) {
				$mail->AddAddress($values['to_email'], $values['to_name']);
			}
		
			if($values['to_cc_email']) {
				$mail->AddCC($values['to_cc_email'], $values['to_cc_name']);
			}			
		
			if($values['to_bcc_email']) {
				$mail->AddBCC($values['to_bcc_email'], $values['to_bcc_name']);
			}		
		}
		
		return $mail;
	}

	
	function sendGeneratedPassword($password, $link, $user) {
		
		$this->letter_key = 'admin_generated_password';

		// parser
		$parser = &$this->parser;
		$parser->assign($user);
		$parser->assign('name', $user['first_name'] . ' ' . $user['last_name']);
		$parser->assign('login', $user['username']);
		$parser->assign('password', $password);
		$parser->assign('link', $link);
		
		
		// mail object
		$mail = &$this->getMailerObj($this->getTemplateVars(false));
		$mail->From = $this->noreply_email;
		$mail->FromName = $this->from_mailer;
		
		$mail->ClearAddresses();
		$mail->AddAddress($user['email']);
		
		$mail->Body = &$parser->parse($this->getTemplate(false));
		
		//echo "<pre>"; print_r($mail); echo "</pre>"; 
		//exit();
		
		return $mail->Send();
	}
}
?>